package com.hospital.hospital.controller;

import com.hospital.hospital.bean.User;
import com.hospital.hospital.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

/**
 * @author xfm
 * @create 2021/9/2
 */

@Controller
@SessionAttributes(types = {User.class})
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
   @RequestMapping("/treatmentUi")
    public String treatmentUi(){
       return "treatment";
   }
   @RequestMapping("/treatment")
    public String treatment(User user, Model model,String have) {
       if (have.equals("新建就诊卡")) {
           try {
                   userService.regist(user);
                   model.addAttribute("user", user);
               return "redirect:/drug/buyUi";
           }catch (Exception e) {
               e.printStackTrace();
               return "redirect:/user/treatmentUi";
           }
       } else {
           try {
               User login = userService.login(user.getIdCard());
               if ((login.getGender().equals(user.getGender()) )&& (login.getAge().equals(user.getAge())) && (login.getName().equals(user.getName()))) {
                   model.addAttribute("user", login);
                   return "redirect:/drug/buyUi";
               }else{
                   return "redirect:/user/treatmentUi";
               }


           } catch (Exception e) {
               e.printStackTrace();
               return "redirect:/user/treatmentUi";
           }

       }
   }
   @RequestMapping("/logout")
    public String logout(SessionStatus sessionStatus){
       sessionStatus.setComplete();
       return "redirect:/manager/indexUi";
   }
}
